package przykladyWykresow;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Timer;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

@SuppressWarnings("serial")
public class WykresDynamiczny1 extends DynamicChartPanelAbstract {
	private final TimeSeries total; // total memory used
	private final TimeSeries free; // free memory
	private ChartPanel chartPanel = null;

	@Override
	public ChartPanel getChartPanel() {
		return chartPanel;
	}

	@SuppressWarnings("deprecation")
	public WykresDynamiczny1(int maxAge) {
		super(new BorderLayout());

		this.total = new TimeSeries("Cała pamięć", Millisecond.class);
		this.total.setMaximumItemAge(maxAge);

		this.free = new TimeSeries("Wolna pamięć", Millisecond.class);
		this.free.setMaximumItemAge(maxAge);
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(this.total);
		dataset.addSeries(this.free);

		DateAxis domain = new DateAxis("Czas");
		NumberAxis range = new NumberAxis("Pamięć");
		domain.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		range.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		domain.setLabelFont(new Font("SansSerif", Font.PLAIN, 14));
		range.setLabelFont(new Font("SansSerif", Font.PLAIN, 14));

		XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
		renderer.setSeriesPaint(0, Color.red);
		renderer.setSeriesPaint(1, Color.green);
		renderer.setStroke(new BasicStroke(3f, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL));
		XYPlot plot = new XYPlot(dataset, domain, range, renderer);
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		domain.setAutoRange(true);
		domain.setLowerMargin(0.0);
		domain.setUpperMargin(0.0);
		domain.setTickLabelsVisible(true);

		range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		JFreeChart chart = new JFreeChart("Wykorzystanie pamięci JVM",
				new Font("SansSerif", Font.BOLD, 24), plot, true);
		chart.setBackgroundPaint(Color.white);
		chartPanel = new ChartPanel(chart);
		chartPanel.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(4, 4, 4, 4),
				BorderFactory.createLineBorder(Color.black)));
		add(chartPanel);
	}

	private void addTotalObservation(double y) {
		this.total.add(new Millisecond(), y);
	}

	private void addFreeObservation(double y) {
		this.free.add(new Millisecond(), y);
	}

	public class DataGenerator extends Timer implements ActionListener {
		public DataGenerator(int interval) {
			super(interval, null);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			long f = Runtime.getRuntime().freeMemory();
			long t = Runtime.getRuntime().totalMemory();
			addTotalObservation(t);
			addFreeObservation(f);
		}
	}

}